const localData = localStorage.getItem('pz_v3pz')

const state = localData ? localData.menu : {
    isCollapse:false,
    selectMenu:[],
    routerList:[]
}

const mutations = {
    collapseMenu(state){
        state.isCollapse = !state.isCollapse
    },
    addMenu(state,payload){
        //对数据进行去重
        if(state.selectMenu.findIndex(item=>item.path===payload.path) === -1){
            state.selectMenu.push(payload)
        }
    },
    closeMenu(state,payload){
        //找到点击数据的索引
        const index = state.selectMenu.findIndex(val=>val.name===payload.name) 
        //通过索引删除数组指定元素
        state.selectMenu.splice(index,1)
    },
    dynamicMenu(state,payload){
        //通过glob导入文件
        const modules = import.meta.glob('../views/**/**/*.vue')
        function routerSet(routes) {  
            routes.forEach(route => {  
                if (!route.children) {  
                    const url = `../views${route.meta.path}/index.vue`;  
                    if (modules[url]) {  
                        route.component = modules[url];  
                    } else {  
                        console.error(`Component not found for path: ${url}`);  
                        // 可以选择在这里设置一个默认的组件或进行其他错误处理  
                        // route.component = DefaultComponent;  
                    }  
                } else {  
                    // 递归调用，确保子路由也被处理  
                    routerSet(route.children);  
                }  
            });  
        } 
        routerSet(payload)
        //拿到完整的路由数据
        state.routerList = payload
    },
    updateMenuActive(state,payload){
        state.menuActive = payload
    }
}

export default {state,mutations}